package cn.shyouchi.wuguanjia.android.ui.casualrecord;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;

import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.casual.CasualApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.model.BaseResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class ChoosePersonActivity extends BaseActivity {
    private String title;
    private String content;
    private ArrayList<String> photo;


    CasualApi mCasualApi;


    ChooseAdapter mChooseAdapter;
    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_persons)
    RecyclerView mRvPersons;


    List<MultiItemEntity> mChooseGroups = new ArrayList<>();

    MenuItem mMenuItem;

    @Override
    public int initContentView() {
        return R.layout.activity_choose_person;
    }

    @Override
    public void initInjector() {
        mCasualApi = new CasualApi(mContext);


    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        mTopBar.setTitle(getTitle().toString());
        setSupportActionBar(mTopBar.getToolbar());


        mChooseAdapter = new ChooseAdapter(mChooseGroups);
        mRvPersons.setAdapter(mChooseAdapter);
        mRvPersons.setLayoutManager(new LinearLayoutManager(mContext));
        fetchPerson();
        initData();

    }

    private void initData() {
        Intent intent = getIntent();
         title = intent.getStringExtra("title");
         content = intent.getStringExtra("content");
        photo=intent.getStringArrayListExtra("photo");
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    private void fetchPerson() {
        mCasualApi.getPersons()
                .subscribeOn(Schedulers.io())
                .map(new Func1<BaseResult<List<ChooseGroup>>, List<ChooseGroup>>() {
                    @Override
                    public List<ChooseGroup> call(BaseResult<List<ChooseGroup>> result) {
                        return result.getResultMessage();
                    }
                })
                .map(new Func1<List<ChooseGroup>, List<ChooseGroup>>() {
                    @Override
                    public List<ChooseGroup> call(List<ChooseGroup> chooseGroups) {
                        for (ChooseGroup chooseGroup : chooseGroups) {
                            for (ChooseBean chooseBean : chooseGroup.getInfo()) {
                                chooseGroup.addSubItem(chooseBean);
                            }
                        }
                        return chooseGroups;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<ChooseGroup>>(mContext) {
                    @Override
                    public void onNext(List<ChooseGroup> chooseGroups) {
                        mChooseGroups.clear();
                        mChooseGroups.addAll(chooseGroups);
                        mChooseAdapter.notifyDataSetChanged();
                    }
                });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        mMenuItem = menu.add("确定");
        mMenuItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item == mMenuItem) {
            sendCasual(title,content, photo,mChooseAdapter.getSelected());


            //onBackPressed();
            //setResult(Constant.ACTIVITY_RESPONSE_SUCCESS,getIntent().putIntegerArrayListExtra("ids", mChooseAdapter.getSelected()));
            // finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    private void sendCasual(String title, String content, ArrayList<String> imgs, ArrayList<Integer> ids) {
        mCasualApi.sendCasual(title, content, ids, imgs)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult requestResult) {
                        System.out.println("信息发布成功！");

                        //startActivity(new Intent(mContext, CasualMainActivity.class));
                        finish();
                       // setResult(Constant.ACTIVITY_RESPONSE_NEED_REFRESH);
                       // finish();
                    }
                });
    }


}
